package framework.java.io;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.zip.GZIPOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import util.file.fileUtil;
public class zip extends fileUtil {
	private static zip instance;
	private static final int buff_size = 1024 * 1024;
	public synchronized static zip getInstance() {
		if (instance == null) {
			instance = new zip();
		}
		return instance;
	}
	public static void bathZipFiles(Collection<File> resfilelist, File zipfile) throws Exception {
		ZipOutputStream ziout = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipfile), buff_size));
		for (File file : resfilelist) {
			byte[] buffer = new byte[buff_size];
			BufferedInputStream in = new BufferedInputStream(new FileInputStream(file), buff_size);
			ziout.putNextEntry(new ZipEntry(file.getName()));
			int reallength;
			while ((reallength = in.read(buffer)) != -1) {
				ziout.write(buffer, 0, reallength);
			}
			in.close();
			ziout.flush();
			ziout.closeEntry();
		}
		ziout.close();
	}
	public static void bathZipFiles1(Collection<File> resfilelist, File zipfile) throws Exception {
		GZIPOutputStream gzip = new GZIPOutputStream(new FileOutputStream(zipfile), buff_size);
		for (File file : resfilelist) {
			byte[] buffer = new byte[buff_size];
			BufferedInputStream in = new BufferedInputStream(new FileInputStream(file), buff_size);
			int reallength;
			while ((reallength = in.read(buffer)) != -1) {
				gzip.write(buffer, 0, reallength);
			}
			in.close();
			gzip.flush();
		}
		gzip.close();
	}
	public static void main(String[] args) {
		Collection<File> resFileList = new ArrayList<File>();
		resFileList.add(new File(file2));
		resFileList.add(new File(file1));
		// resFileList.add(new File("E://im//user_last.xml"));
		// File zipFile = new File("e://im//depuser.zip");
		File zipFile = new File("C:\\Users\\K\\Desktop\\1.zip");
		try {
			bathZipFiles1(resFileList, zipFile);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
